<?php
namespace app\common\validate\jy;

class OrderRefund extends Base {
	/**
     * 验证规则
     */
    protected $rule = [
    	'lock'          => 'require',
        'id'            => 'require|isUser:jy\\OrderRefund|isRefundOwner',
        'uid'           => 'require',
        'sid'           => 'require',
        'aid'           => 'require|hasAdminOperation:order_refund',
        'oid'           => 'require|isUser:jy\\Order|isPay|isRefund|hasRefund',
        'opids'         => 'require|opids|isOstSale',
        'nums'          => 'require|nums',
        'remark'        => 'max:200',
        'iids'          => 'iids',
        'operate'       => 'require|isStoreHandleOperate',
        'expressNumber' => 'require'
    ];
    /**
     * 提示消息
     */
    protected $message = [
    	'lock.require'                 => '验证规则场景不存在',
        'id.require'                   => '退款单ID不能为空',
        'id.isUser'                    => '退款单不属于您',
        'id.isRefundOwner'             => '退款单不属于您的店铺',
        'id.isStatus'                  => '退款单状态已改变',
        'uid.require'                  => '用户ID不能为空', 
        'sid.require'                  => '店铺ID不能为空',
        'aid.require'                  => '管理员未登录',
        'aid.hasAdminOperation'        => '管理员权限不足',
        'oid.require'                  => '订单ID不能为空',
        'oid.isUser'                   => '订单不属于您',
        'oid.isPay'                    => '订单尚未支付',
        'oid.isRefund'                 => '订单已超过退款时效',
        'oid.hasRefund'                => '订单存在尚未结束的退款单',
        'opids.require'                => '退款产品ID不能为空',
        'opids.opids'                  => '退款产品和退款数量不匹配',
        'opids.isOstSale'              => '包含售后不支持退款的产品',
        'num.require'                  => '退款产品数量不能为空',
        'nums.nums'                    => '产品退款数量大于可退款数量',
        'remark.max'                   => '备注最多不能超过200个字符',
        'iids.iids'                    => '上传图片ID不合法',
        'operate.require'              => '操作不能为空',
        'operate.isStoreHandleOperate' => '操作不合法',
        'expressNumber.require'        => '快递单号不能为空'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'refund'                                => ['oid', 'uid', 'opids', 'nums', 'iids'],
        'private'                               => ['uid'],
        'store'                                 => ['sid'],
        'user_info'                             => ['id' => 'require|isUser:jy\\OrderRefund', 'uid'],
        'store_info'                            => ['id' => 'require|isRefundOwner', 'sid'],
        'store_handle'                          => ['id' => 'require|isRefundOwner|isStatus:senqingtuikuang', 'sid', 'operate' => 'require|isStoreHandleOperate', 'iids'],
        'user_return_product'                   => ['id' => 'require|isUser:jy\\OrderRefund|isStatus:dengdaimaijiatuihuo', 'uid', 'expressNumber'],
        'store_received_return'                 => ['id' => 'require|isRefundOwner|isStatus:tuihuozhong', 'sid', 'iids'],
        'store_refund_product_handle'           => ['id' => 'require|isRefundOwner|isStatus:maijiayiqianshou', 'sid', 'operate', 'iids'],
        'apply_for_administrator_intervention'  => ['id' => 'require|isUser:jy\\OrderRefund|isStatus:maijiajujuetuikuan', 'uid'],
        'administrator_hanlde'                  => ['id' => 'require|isStatus:shenqingguanliyuanjieru', 'aid'],
        'user_return_product2'                  => ['id' => 'require|isUser:jy\\OrderRefund|isStatus:dengdaimaijiatuihuo2', 'uid', 'expressNumber'],
        'store_received_return2'                => ['id' => 'require|isRefundOwner|isStatus:tuihuozhong2', 'sid', 'iids'],
        'store_refund_product2_handle'          => ['id' => 'require|isRefundOwner|isStatus:maijiayiqianshou2', 'sid', 'operate', 'iids'],
        'apply_for_administrator_intervention2' => ['id' => 'require|isUser:jy\\OrderRefund|isStatus:tuihuomaijiajujue,tuihuomaijiajujue2', 'uid'],
        'administrator_hanlde2'                 => ['id' => 'require|isStatus:shenqingguanliyuanjieru2', 'aid'],
    ];

    //退款单是否属于店铺
    protected function isRefundOwner($value, $rule, $data = []) {
        $refund = loadOnly('jy\\OrderRefund')->find('jy\\OrderRefund', $value);
        $order = loadOnly('jy\\Order')->find('jy\\Order', $refund['oid']);
        return $order['sid'] == $data['sid'];
    } 

    //订单是否支付
    protected function isPay($value, $rule, $data = []) {
        $order = loadOnly('jy\\OrderProduct')->find('jy\\Order', $value);
        return $order['paytime'] > 0;
    }

    //订单是否支持退款
    protected function isRefund($value, $rule, $data = []) {
        $only = loadOnly('jy\\OrderProduct');
        $order = $only->find('jy\\Order', $value);
        $time = time();
        if($order['receive_time'] != 0 && $order['receive_time'] + 7 * 24 * 3600 < $time) {
            return false;
        }
        $profitSharing = $only->getModel('jy\\ProfitSharing')->where(['oid' => $value])->find();
        return empty($profitSharing);
    }

    //产品ID和退款数量是否匹配
    protected function opids($value, $rule, $data = []) {
        return count($data['opids']) == count($data['nums']);
    }

    //是否存在未处理的退款单
    protected function hasRefund($value, $rule, $data = []) {
        $only = loadOnly('jy\\OrderRefund');
        $count = $only->hasRefund($value);
        return $count == 0;
    }

    //退款产品是否支持退款售后服务
    protected function isOstSale($value, $rule, $data = []) {
        $only = loadOnly('jy\\OrderProduct');
        $ostSaleEnum = loadEnum('OstSale');

        $list = $only->refund4oid($data['oid']);
        $opids = $data['opids'];

        foreach($opids as $k => $opid) {
            if(!isset($list[$opid])) {
                return false;
                break;
            }
            $snapshotProduct = $only->find('jy\\SnapshotProduct', $list[$opid]['snapshotid']);
            if(!$ostSaleEnum->isRefund($snapshotProduct['osid'])) {
                return false;
                break;
            }
        }
        return true;
    }

    //退款数量是否超过可退款数量
    protected function nums($value, $rule, $data = []) {
        $only = loadOnly('jy\\OrderProduct');
        $orderRefundProductOnly = loadOnly('jy\\OrderRefundProduct');

        $list = $only->refund4oid($data['oid']);
        $opids = $data['opids'];
        $nums = $data['nums'];

        foreach($opids as $k => $opid) {
            $item = $list[$opid];
            $num = $nums[$k];
            $refundNum = $orderRefundProductOnly->getRefundNum($opid);
            if($num + $refundNum > $item['num']) {
                return false;
            }
        }
        return true;
    }

    //退款操作图片集是否合法
    protected function iids($value, $rule, $data = []) {
        foreach($value as $k => $iid) {
            if(!ctype_digit($iid)) {
                return false;
                break;
            }
        }
        return true;
    }

    //判断是否是正常的商家操作
    protected function isStoreHandleOperate($value, $rule, $data = []) {
        if(!$rule) {
            $rule = 'refuse,agree';
        }
        $rule = explode(',', $rule);
        return in_array($value, $rule);
    }

    //判断是否是指定状态的订单
    protected function isStatus($value, $rule, $data = []) {
        $enum = loadEnum('OrderRefundStatus');
        $only = loadOnly('jy\\OrderProduct');
        $row = $only->find('jy\\OrderRefund', $value);
        $arr = explode(',', $rule);
        foreach($arr as $k => $v) {
            if($row['status'] == $enum->code2key($v)) {
                return true;
                break;
            }
        }
        return false;
    }
}
